<?php

/**
 * $Id: IndexController.php 423 2008-05-30 14:42:41Z dakl $
 * Strona glowna
 *
 */
class PageController extends Controller {

	/**
	 * Domyslna metoda
	 *
	 */
	public function IndexAction($param) {

		//USTALENIE WARTOSCI
		if(!isset($_REQUEST['published'])) $_REQUEST['published'] = 0; else $_REQUEST['published'] = 1;
		if(!isset($_REQUEST['sort'])) $_REQUEST['sort'] = 0;

		//DODANIE NOWEJ STRONY
		$this->smarty->Assign('msg', null);
		if(isset($_REQUEST['AddNewPage'])) {
			$exsists = PageDAL::getResult(array('id_type' => $_REQUEST['type'], 'id_type_action' => $_REQUEST['typeAction']), 1);
			
			if(count($exsists) < 1 || ($_REQUEST['type'] == 0 && $_REQUEST['typeAction'] == -1)) {
				$objPage = new Page(-1, $_REQUEST['title'], $_REQUEST['content'], $_REQUEST['published'], $_REQUEST['sort'], $_REQUEST['short_content'], $_REQUEST['type'], $_REQUEST['typeAction']);
				//if($_FILES['photo'] != NULL) {
				//	PhotoDAL::FileUpload($_FILES['photo'], $objPage, null, null);
				//}
				PageDAL::Save($objPage, null);
				PhotoDAL::FileUpload($_FILES['photo'], $objPage, null, null);
			} else {
				$this->smarty->assign('msg', 'Strona o takiej kategorii i typie ju� istnieje!');
			}
		}

		//AKTUALIZACJA STRONY
		if(isset($_REQUEST['UpdatePage'])) {
			
			$objPage = new Page($_REQUEST['id_wp_page'], $_REQUEST['title'], $_REQUEST['content'], $_REQUEST['published'], $_REQUEST['sort'], $_REQUEST['short_content'], $_REQUEST['type'], $_REQUEST['typeAction']);
			PageDAL::Save($objPage, null);
			PhotoDAL::FileUpload($_FILES['photo'], $objPage, null, null);
		}

		//USUNIECIE STORNY
		if(isset($param['delete'])) {
			PageDAL::Delete($param['id']);

		}

		//SORTOWANIE
		$activeSort = array('sort' => '', 'title' => '', 'published' => '');

		if (isset($param['sd'])) {
			$sortDirection = $param['sd'];
			if ($sortDirection == 'down') {
				$sortDirectionParm = 'ASC';
			} else {
				$sortDirectionParm = 'DESC';
			}
		} else {
			$sortDirection = 'up';
			$sortDirectionParm = 'DESC';
		}
		if (isset($param['sc'])) {
			$sortColumn = $param['sc'];
		} else {
			$sortColumn = 'id_wp_page';
		}
		$activeSort[$sortColumn] = '_active';

		$this->smarty->assign('sortDirection', $sortDirection);
		$this->smarty->assign('sortColumn', $sortColumn);
		$this->smarty->assign('activeSort', $activeSort);

		//WYSZUKIWANIE
		if (isset($_REQUEST["searchString"])) {
			SessionProxy::SetValue('request', $_REQUEST);

		} else if (!(isset($param["page"]))) {
			if (!(isset($param["sd"])) || !(isset($param["sc"]))) {
				SessionProxy::ClearValue('request');
			}
		}
		$request = SessionProxy::GetValue('request');

		if (isset($request["searchString"])) { $searchString = trim($request["searchString"]); } else { $searchString = ""; }
		$this->smarty->assign('searchString', $searchString);

		//PEJDZOWANIE
		if (!isset($param['page'])) {
			$param['page'] = 0;
		}

		$countAll = PageDAL::GetCountAll(null,$request);
		$countLimit = 20;
		$offset = $param['page'];

		$this->smarty->assign('countAll', $countAll);
		$this->smarty->assign('countLimit', $countLimit);
		$this->smarty->assign('pageOffset', $param['page']);

		//AKCJE DOMYSLNE
		$sortColumn = "id_type DESC, id_type_action ";
		$sortDirection = "ASC";
		$logger = LoggerManager::getLogger(__CLASS__);
		$this->smarty->assign('pagesArrayObj', PageDAL::GetAllPages($offset, $countLimit, $request, $searchString, $sortColumn, $sortDirectionParm, $request));

	}

	/**
	 * Edycja/dodawanie strony
	 *
	 * @param array $param
	 */
	public function EditAction($param) {

		include("plugins/fckeditor/fckeditor_php5.php");
		$oFCKeditor = new FCKeditor('content');
		$oFCKeditor->BasePath = 'plugins/fckeditor/';
		$oFCKeditor->Value = '';
		$oFCKeditor->ToolbarSet = 'Formix';
		$oFCKeditor->Height = 400;

		$shortContent = new FCKeditor('short_content');
		$shortContent->BasePath = 'plugins/fckeditor/';
		$shortContent->Value = '';
		$shortContent->ToolbarSet = 'Basic';
		$shortContent->Height = 100;
		
		if(isset($param['id'])) {
			$page = PageDAL::GetPageById($param['id']);
			
			$this->smarty->assign('titleType', 'Edytuj');
			$this->smarty->assign('pageObj', $page);
			$oFCKeditor->Value = stripslashes($page->GetContent());
			$shortContent->Value = stripcslashes($page->GetShortContent());
			
		} else {
			$this->smarty->assign('titleType', 'Dodaj');
		}

		$this->smarty->assign('fck', $oFCKeditor);
		$this->smarty->assign('shortContent', $shortContent);
		$typ = new Type();
		//$typy =  $typ->GetFreeTypeNamesPL();
		$typy =  $typ->GetAllTypeNamePL();
		$typy[0] = "Nie dotyczy";
		$this->smarty->assign('typy', $typy);
		
		$typyAction = $typ->GetAllTypeActionIdNamePL();
		$typyAction[-1] = "Nie dotyczy";
		$this->smarty->assign('typyAction', $typyAction);
	}

	/**
	 * Zapsiuje sortowanie
	 *
	 */
	public function SortAction($param) {

		if(isset($_REQUEST['arraySort'])) {
			Utils::SaveSort($_REQUEST['arraySort'], 'sort', 'wp_page', 'id_wp_page');
		}
		die(header("Location: index,page".APPLICATION_FILE_TYPE));
	}

	/**
	 * Metoda wspolna
	 *
	 */
	public function preDispatch() {
		$this->RunShared('Auth');
		$this->smarty->assign('timestamp', microtime() );
	}
}
